package com.icinema.main;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.icinema.client.Constants;
import com.icinema.client.ICinema;
import com.icinema.client.MovieService;
import com.icinema.client.MovieServiceAsync;
import com.icinema.vo.MovieShowingDetail;

public class GeneralMoviesListView extends Composite {
	ICinema iCinema;
	ListBox lstDuration = new ListBox();
	private final MovieServiceAsync movieService = GWT
	.create(MovieService.class);
	int currentSelected = 1;
	
	FlexTable rows = new FlexTable();
	
	@SuppressWarnings("deprecation")
	public GeneralMoviesListView(){
		VerticalPanel panel = new VerticalPanel();
		HorizontalPanel hPanel = new HorizontalPanel();
		hPanel.setSpacing(10);
		initWidget(panel);
		Hyperlink btnLogin = new Hyperlink();
		btnLogin.setText("Login");
		Hyperlink btnRegister = new Hyperlink();
		btnRegister.setText("Register");
		btnRegister.setStyleName("hyper-link");
		btnLogin.setStyleName("hyper-link");
		btnRegister.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				iCinema.showUserRegistrationViewCallBack();
			}
			
		});
		btnLogin.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				iCinema.showLoginViewCallBack();
			}
			
		});
		
		lstDuration.addItem("Since Last 2 Weeks",Constants.TWO_WEEKS+"");
		lstDuration.addItem("This month",Constants.THIS_MONTH+"");
		lstDuration.addChangeHandler(new ChangeHandler(){
			@Override
			public void onChange(ChangeEvent event) {
				currentSelected = Integer.parseInt(lstDuration.getValue(lstDuration.getSelectedIndex()));
				loadShowList(currentSelected);
			}
			
		});


		
		
		hPanel.add(btnLogin);
		hPanel.add(btnRegister);
		panel.add(lstDuration);
		panel.add(rows);
		panel.add(hPanel);
	}
	
	public void loadShowList(int duration){
			rows.clear();
			rows.clear(true);
			final PopupPanel popup = new PopupPanel();
			popup.setTitle("Loding..");
			popup.setWidget(new Label("Please Wait While Loading"));
			popup.setAutoHideEnabled(false);
			popup.setModal(true);
			popup.show();
			movieService.getMovieShowings(currentSelected,new AsyncCallback<List<MovieShowingDetail>>(){

			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(List<MovieShowingDetail> result) {
				rows.removeAllRows();
				rows.getRowFormatter().setStyleName(0, "tableViewBrowseHeaders");
				rows.setText(0,0,"Cinema");
				rows.setText(0,1,"Movie");
				rows.setText(0,2,"Showing From Date");
				rows.setText(0,3,"Showing To Date");
				CellFormatter formatter = rows.getCellFormatter();
				for(int i=0; i < result.size();i++ ){
						formatter.setStyleName(i+1,0,"tableCell");
						formatter.setStyleName(i+1,1,"tableCell");
						formatter.setStyleName(i+1,2,"tableCell");
						formatter.setStyleName(i+1,3,"tableCell");
						rows.setText(i+1,0,result.get(i).getCinema().getName());	
						rows.setText(i+1,1, result.get(i).getMovie().getName());
						rows.setText(i+1,2, result.get(i).getMovieShowing().getShowingFromDate());
						rows.setText(i+1,3, result.get(i).getMovieShowing().getShowingToDate());
				}
				popup.hide();
			}
			
		});
	}


	
	public void refresh(){
		this.setVisible(false);
		this.setVisible(true);
	}	
	
	@Override
	public void setVisible(boolean visible) {
		if(visible == true){
			loadShowList(currentSelected);
		}
		super.setVisible(visible);
	}

	public void setICinema(ICinema icCinema){
		this.iCinema = icCinema;
	}
}
